#include "letterdialogimpl.h"
#include "loginUi.h"

extern loginUi *login;

letterdialogImpl::letterdialogImpl( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
	setupUi(this);
	connect(tb_let,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(tableClicked(int,int)));
	connect(pushButton,SIGNAL(clicked()),this,SLOT(onClick()));
}


void letterdialogImpl::onClick() {
	login->win.getHistoricFromWell(lb_name->text());
}
void letterdialogImpl::addAux(int row, int col, QString str) {
	printf("ADICIONANDO ITEM %s\n",str.toStdString().c_str());
	if(!tb_let->item(row,col)) {
		QTableWidgetItem *tmpItem = new QTableWidgetItem(str);
		tmpItem->setFlags(Qt::ItemIsEnabled);
		tmpItem->setTextAlignment(Qt::AlignCenter);
		tb_let->setItem(row,col,tmpItem);;
	} else
		tb_let->item(row,col)->setText(str);
}

void letterdialogImpl::setup(QStringList letters){
	QString Par,wellId;
	int numberOfLetters;
	wellId = letters.front(); letters.pop_front();
	numberOfLetters = letters.front().toInt(); letters.pop_front();
	
	lb_name->setText(wellId);
	tb_let->setRowCount(numberOfLetters);
	printf("NUMERO DE CARTAS %i\n",	numberOfLetters);
	pointsList.clear();
	tab_letter->setCurrentIndex(0);
	for(int i = 0; i < numberOfLetters; i++){
		if(letters.size() <8)
			break;
		addAux(i,0,letters.front()); letters.pop_front();
		addAux(i,1,letters.front()); letters.pop_front();
		addAux(i,2,letters.front()); letters.pop_front();
		addAux(i,3,letters.front()); letters.pop_front();
		addAux(i,4,letters.front()); letters.pop_front();
		addAux(i,5,letters.front()); letters.pop_front();
		pointsList.push_back(letters.front()); letters.pop_front();
		addAux(i,6,letters.front()); letters.pop_front();
	}
	tb_let->resizeColumnsToContents();
	this->show();
}

void letterdialogImpl::tableClicked(int row, int) {
	QStringList points = getPaintOfDbLetter(pointsList[row]);
	
	printf("numero de pontos %i\n",points.size());
	for(int i = 0; i < 200; i++) {
		x[i] = points.front().toInt(); points.pop_front();
		y[i] = points.front().toInt(); points.pop_front();
		printf("x = %f y = %f \n",x[i],y[i]);
	}
	curve.setRawData(x,y,200);	
	curve.attach(p_letter);
	tab_letter->setCurrentIndex(1); 
	p_letter->replot();
}


int letterdialogImpl::NumberOfDigits(int n) {
	int i = 10;
	int out = 1;
	while(n / i > 0) {
		i *= 10;
		out++;
	}
	return out;
}

QStringList letterdialogImpl::getPaintOfDbLetter(QString Points) {
	QStringList ToReturn;
	char *aux;
	Points[Points.length()] = '\0';
	const char * tmp = Points.toStdString().c_str();
	tmp++;
	
	char teste[Points.length()];
	char * t = (char *)teste;
	memset(teste,'\0',Points.length());
	memcpy(teste,Points.toStdString().c_str(),Points.length());
	teste[strlen(teste) - 1] = ',';
	t += 1;
	printf("TGAMANHO %i\n",strlen(tmp));
	printf("%s\n",t);
	aux = strtok((char *) t,",");	
		
	
	int i = 0;
	printf("______________________\n");
	while(aux != NULL) {
		printf("%s ",aux);
		ToReturn.push_back(aux);
		i++;
		aux = strtok(NULL,",");
	}
	printf("______________________\n");
	printf("TAMANHO ANTES DE SAIR %i %i\n",ToReturn.size(),i);
	return ToReturn;
}
